#include "mainwindow.h"

#include <QApplication>

//int main(int argc, char *argv[])
//{
//    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();
//    return a.exec();
//}

#include <QtWebEngineWidgets>
#include <QDebug>
#include <QObject>

//struct ScriptCallback
//{
//    void operator()(const QVariant &result)
//    {
//        qDebug()<<result.toString();
//    }
//};



#include "zzz.h"// QObject的子类，若是没有在"?.h"文件中声明(在"?.cpp"中声明的话)，就会报错 ！！！...

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebEngineView *pView = new QWebEngineView();
    pView->setUrl(QUrl("E:/Gitee/Qt_Test/Test/Qt5142_vs17_01/zz_test_01.html"));
    pView->show();

    ZZ* zz = new ZZ();
    zz->pView = pView;
    QWebEnginePage* pPage = pView->page();
    QObject::connect(pPage, &QWebEnginePage::loadFinished, zz, &ZZ::LoadFinished);

    return a.exec();
}


//void ZZ::LoadFinished(bool ok)
//{
//    qDebug()<<"LoadFinished(...) - " << ok;

//    QWebEnginePage* pPage = pView->page();
//    pPage->runJavaScript("function myFunction() {"
//                         "var AA = document.getElementById('AA01');"
//                         "return AA.href;} myFunction();",
//                         [] (const QVariant &result) {
//        qDebug()<<result.toString();
//    });
//}

#include <QVariant>

void ZZ::LoadFinished(bool ok)
{
    qDebug()<<"LoadFinished(...) - " << ok;

    QWebEnginePage* pPage = pView->page();
    pPage->runJavaScript("function myFunction() {"
                         "  var AA = document.getElementsByTagName('a');"
                         "  var hrefs = [];"
                         "  for( var i = 0; i < AA.length; i++)"
                         "     hrefs[i] = AA[i].href;"
                         "  console.log(hrefs);"
                         "  return hrefs;"
                         "} myFunction();",
                         [] (const QVariant &result) {
        qDebug()<<result.toString();
        qDebug()<<result;
        if (result.type() == QVariant::Type::List)
        {
            QVariantList* pVarList = (QVariantList*)&result;
            if (pVarList->count()>0)
                for (int i=0; i<pVarList->count(); i++)
                {
                    QVariant vv = pVarList->at(i);
                    if (vv.type() == QVariant::Type::String)
                    {
                        qDebug() << i << ":" <<vv.toString();
                    }
                }
        }
    });
}
